<?php

namespace App\Http\Controllers;

use App\Models\NewsVideoModel;
use App\Models\VideoLikeModel;
use App\Sevrice\ApiJwt;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class NewsVideoController extends Controller
{
    /**
     * @param Request $request
     * @return array
     * 分页查询段视频id、标题、点赞数、浏览量、短视频封面等数据
     */
    function newsVideoList(Request $request)
    {
        $data = NewsVideoModel::select(['video_id', 'video_title', 'gain_like_num', 'view_num', 'video_cover'])->paginate(15)->toArray();
        $last_page = $data['last_page']; //数据总页码数
        //将数据根据id奇偶性赋值给两个数据中用户前端双列渲染
        $oddData = [];
        $evenData = [];
        foreach ($data['data'] as $v) {
            if ($v['video_id'] % 2 != 0) {
                array_push($oddData, $v);
            } else {
                array_push($evenData, $v);
            }
        }
        return ['code' => 200, 'mag' => '查询成功', 'data' => ['odd' => $oddData, 'even' => $evenData, 'last_page' => $last_page]];
    }

    /**
     * @param Request $request
     * @return mixed
     * 查询对应id的短视频信息发布用户id、头像、名称及短视频id、标题、获得点赞、转发、评论数量、
     * 短视频云存储链接、发布时间、修改时间等详情信息以及目标短视频的上一条短视频id及下一条短视频id
     *
     */
    function videoInfo(Request $request)
    {
        $id = $request->post('videoId');
//        $userId=1;
        $userId=ApiJwt::decodeToken($request->header('token'));
        $dataInfo = NewsVideoModel::with(['issueUserInfo' => function ($query) {
            return $query->select('user_id', 'user_logo', 'user_nick');
        }])->where('video_id', $id)->first(['video_id', 'video_title', 'short_video', 'gain_like_num', 'transmit_num', 'remark_num', 'user_num', 'created_at', 'updated_at'])->toArray();
        //将添加、修改时间由2022-08-25T20:03:46.000000Z格式替换成2022-08-25 20:03:46标准时间格式
        $dataInfo['created_at'] = substr($dataInfo['created_at'], 0, 10) . ' ' . substr($dataInfo['created_at'], 11, 8);
        $dataInfo['updated_at'] = substr($dataInfo['created_at'], 0, 10) . ' ' . substr($dataInfo['created_at'], 11, 8);
        //目标短视频id的上一条数据id
        $prev = NewsVideoModel::where('video_id', '<', $id)->max('video_id');
        //目标短视频id的下一条数据id
        $next = NewsVideoModel::where('video_id', '>', $id)->min('video_id');
        $dataInfo['prevId'] = $prev;
        $dataInfo['nextId'] = $next;
        $min = NewsVideoModel::get()->min('video_id');//查询短视频最小的id
        $max = NewsVideoModel::get()->max('video_id');//查询短视频最大的id
        //如果短视频上一条或者下一条没有数据了则在最小值-最大值间随机赋值
        if ($dataInfo['prevId'] == null ) {
            $dataInfo['prevId'] = mt_rand($min, $max);
        }
        if($dataInfo['nextId'] == null){
            $dataInfo['nextId'] = mt_rand($min, $max);
        }
        //查询点赞表中的否有数据
        $err=VideoLikeModel::where('video_num',$id)->where('user_num',$userId)->value('like_id');
        if($err){
            $dataInfo['isLike']=1;
        }else{
            $dataInfo['isLike']=0;
        }
        return $dataInfo;
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     * 实现用户为指定短视频点赞
     */
    function dotLike(Request $request)
    {
        $id = $request->post('videoId');
        $userId=ApiJwt::decodeToken($request->header('token'));
        $arr['video_num']=$id;
        $arr['user_num']=$userId;
        $arr['like_time']=time();
        $arr['created_at']=date('Y-m-d H:i:s');
        DB::beginTransaction();
        try {
            $res=DB::table('sf_video_like')->insertGetId($arr);
            if($res){
              DB::table('news_video')->where('video_id',$id)->increment('gain_like_num');
            }
            DB::commit();
        }
        catch(\Illuminate\Database\QueryException $ex) {
            DB::rollback();
            return \Response::json(['code' => 400, 'error_msg' =>$ex]);
        }
        $likeNum=NewsVideoModel::where('video_id',$id)->value('gain_like_num');
        return \Response::json(['code' =>200,'msg'=>'点赞成功','data'=>$likeNum]);
    }

    function delLike(Request $request)
    {
        $id = $request->post('videoId');
        $userId=1;
//        $userId=ApiToken::decodeToken($request->header('token'));
        DB::beginTransaction();
        try {
            $res=DB::table('sf_video_like')->where('video_num',$id)->where('user_num',$userId)->delete();
            if($res){
                DB::table('news_video')->where('video_id',$id)->decrement('gain_like_num');
            }
            DB::commit();
        }
        catch(\Illuminate\Database\QueryException $ex) {
            DB::rollback();
            return \Response::json(['code' => 400, 'error_msg' =>$ex]);
        }
        $likeNum=NewsVideoModel::where('video_id',$id)->value('gain_like_num');
        return \Response::json(['code' =>200,'msg'=>'取消成功','data'=>$likeNum]);
    }

}
